import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;


public class Station {
	
	private int id;
	
	private ArrayList<Integer> vehiclesOnRadius;
	
	private Location local;
	
	private String _address;
	
	private int _port;
	
	private Socket socket;
	
	private PrintWriter EmulOut;
	
	private PrintWriter SimOut;
	
	private double lat;
	private double lon;
	
	
	public Station(int ide, double latitude, double longitude, int emulport, PrintWriter out, String address, int netport){
		
		this.id = ide;
		
		this.SimOut = out;
		
		this._address = address;
		
		this._port = netport;
		
		this.vehiclesOnRadius = new ArrayList<Integer>();
		
		this.lat = latitude;
		
		this.lon = longitude;
		
		this.local = new Location(latitude, longitude, 0);
		
		
		try {
			
			this.socket = new Socket(_address, emulport);
			
			this.EmulOut = new PrintWriter(this.socket.getOutputStream(), true);
			
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public PrintWriter getEmulOut(){
		
		return this.EmulOut;
	}
	
	public PrintWriter getSimOut(){
		
		return this.SimOut;
	}
	
	public void Start(){
		
		//fazer merdas
	}
	
	public int getID(){
		
		return this.id;
	}
	
	public double getLatitude(){
		
		return this.lat;
	}
	
	public double getLongitude(){
		
		return this.lon;
	}
	
	public Location getLocation(){
		
		return this.local;
	}
	
public String getAddress(){
		
		return this._address;
	}
	
	public int getPort(){
		
		return this._port;
	}

	public ArrayList<Integer> getVehiclesInRadius() {
		return vehiclesOnRadius;
	}

	public void setVehiclesInRadius(ArrayList<Integer> vehiclesInRadius) {
		this.vehiclesOnRadius = vehiclesInRadius;
	}
	
	
	public void addVehicleOnRadius(int id){
		
		this.vehiclesOnRadius.add(id);
	}
	
	public void rmVehicleOnRadius(int id){
		for(int i=0; i<vehiclesOnRadius.size(); i++){
			if (vehiclesOnRadius.get(i).equals(id))
				vehiclesOnRadius.remove(i);
		}
		
	}
	
	public boolean isVehicleOnRadius(int id){
		for(int i=0; i<vehiclesOnRadius.size(); i++){
			if (vehiclesOnRadius.get(i).equals(id))
				return true;
		}
		return false;
	}
	
	public void kill(){
		
		try {
			
			this.socket.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
